package com.androidhive.jsonparsing;

import android.app.Activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.LoggingPermission;

import javax.security.auth.login.LoginException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.util.Log;

public class LastestNews extends Activity {

	// url to make request
	private static String url = "http://lexbros.com/api_android/news.php";

	// JSON Node names
	private static final String TAG_NEWS = "news";
	private static final String TAG_ID = "id";
	private static final String TAG_NAME = "name";
	private static final String TAG_THUMB = "thumb";
	private static final String TAG_DATE = "date";
	JSONArray news = null;
	ListView listview;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);
		
		ListView listView = (ListView) findViewById(R.id.mylist);
		// Hashmap for ListView
		ArrayList<HashMap<String, String>> newslist = new ArrayList<HashMap<String, String>>();
		// Creating JSON Parser instance
		JSONParser jParser = new JSONParser();

		// getting JSON string from URL
		JSONObject json = jParser.getJSONFromUrl(url);
		try {
			// Getting Array of Contacts
			news = json.getJSONArray(TAG_NEWS);

			// looping through All Contacts
			for (int i = 0; i < news.length(); i++) {
				JSONObject c = news.getJSONObject(i);

				// Storing each json item in variable
				String id = c.getString(TAG_ID);
				String name = c.getString(TAG_NAME);
				String thumb = c.getString(TAG_THUMB);
				String date = c.getString(TAG_DATE);

				// creating new HashMap
				HashMap<String, String> map = new HashMap<String, String>();
				Log.i(id,name);
				// adding each child node to HashMap key => value
				map.put(TAG_ID, id);
				map.put(TAG_NAME, name);
				map.put(TAG_THUMB, thumb);
				map.put(TAG_DATE, date);
				// adding HashList to ArrayList
				newslist.add(map);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}
}
